//
//  GYHomeViewController.swift
//  Weibo
//
//  Created by vane on 16/8/9.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

private let GYHomeViewCellId = "GYHomeViewCellId"

class GYHomeViewController: GYVisitorViewController {

     private lazy var statusListViewModel:GYStatusListViewModel = GYStatusListViewModel()
    
    // 刷新控件
    let refreshControl = UIRefreshControl()

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !isLogin {
            
            visitorView?.setupVisitorViewInfo(nil, title: nil)
            
            return
        }
        
        setupTableView()
        
        loadData()
        
    }
    private func setupTableView(){
        
        tableView.dataSource = self
        
        tableView.registerClass(GYStatusCell.self, forCellReuseIdentifier: GYHomeViewCellId)
        
        tableView.rowHeight = UITableViewAutomaticDimension

        tableView.estimatedRowHeight = 200
        
        tableView.tableFooterView = activityView
        
        tableView.delegate = self
        
        // 添加事件
        refreshControl.addTarget(self, action: "refreshAction", forControlEvents: UIControlEvents.ValueChanged)
        // 添加到tableView身上
        tableView.addSubview(refreshControl)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "backHomeVC", name: "send", object: nil)
   
    }
    
    func backHomeVC(){
        
        loadData()
        
        self.tableView.setContentOffset(CGPoint(x: 0, y: -64), animated: true)
    }
    
    deinit{
        
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    func refreshAction(){
        print("请求数据")
        loadData()
    }
    private lazy var activityView: UIActivityIndicatorView = {
        
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        
        view.color = AppThemeColor
        
        return view
    }()

   
    
}

extension GYHomeViewController:UITableViewDataSource,UITableViewDelegate{
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusListViewModel.dataArray.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(GYHomeViewCellId, forIndexPath: indexPath) as! GYStatusCell
        
        cell.statusViewModel = statusListViewModel.dataArray[indexPath.row]
        
        
        return cell
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.row + 1 == statusListViewModel.dataArray.count && !activityView.isAnimating() {

            activityView.startAnimating()

            loadData()
        }
    }
    
}

extension GYHomeViewController{
    
    
    func loadData(){
        
        statusListViewModel.loadData(activityView.isAnimating()) { (isSuccess) -> () in
            self.refreshControl.endRefreshing()
            self.activityView.stopAnimating()
            if !isSuccess {
                print("请求数据失败")
                return
            }
            
            // 请求数据成功
            print("请求数据成功")
            // 刷新UI
            self.tableView.reloadData()

        }
    }
}